<!-- 文件管理 -->
<template>
  <el-container>
    <el-main>
      <el-card>
        <el-upload
          drag
          class="upload-demo"
          action="/api/upload"
          :on-change="changeFile"
        >
          <el-icon class="el-icon--upload"><upload-filled /></el-icon>

          <div class="el-upload__text">点击选择文件或将文件拖拽到此处</div>
        </el-upload>
      </el-card>

      <!-- 搜索 -->
      <el-card>
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="文件名称">
            <el-input
              v-model="formInline.filename"
              placeholder="请输入文件名称"
            />
          </el-form-item>

          <el-form-item label="文件类型">
            <el-input
              v-model="formInline.filetype"
              placeholder="请输入文件类型"
            />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="getList">搜索</el-button>
            <el-button @click="reset">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>

      <el-card>
        <!-- 表格 -->
        <div class="list">
          <div v-for="(item, index) in list" :key="index" class="list-item">
            <el-image :src="item.url"> </el-image>
            <div class="item-info">
              <p>{{ item.filename }}</p>
              <p>上传于{{ item.createtime }}</p>
            </div>
          </div>
        </div>

        <!-- 分页器 -->
        <el-pagination
          layout="prev, pager, next"
          :total="total"
          @current-change="currentChange"
        >
        </el-pagination>
      </el-card>
    </el-main>
  </el-container>
</template>

<script setup>
import { UploadFilled } from "@element-plus/icons-vue";
import { apiFile } from "@/api/file";
import { ref, onMounted } from "vue";

/**************************************************/

const list = ref([]);

const page = ref({
  current: 1,
  size: 10,
});

const total = ref(0);

const formInline = ref({});

/**************************************************/

// 重置
const reset = () => {
  formInline.value = {};

  getList();
};

// 切换分页
const currentChange = (pageIndex) => {
  page.value.current = pageIndex;

  getList();
};

/**************************************************/

const changeFile = () => {
  getList();
};

/**************************************************/

const getList = () => {
  apiFile({ ...page.value, ...formInline.value }).then((res) => {
    list.value = res.data;

    total.value = res.total;
  });
};

onMounted(() => {
  getList();
});
</script>

<style lang="scss">
.el-upload {
  width: 100%;
  .el-upload-dragger {
    width: 100%;
  }
}

.list {
  flex: 1;
  width: 100%;
  display: flex;
  flex-wrap: wrap;
}

.list-item {
  flex-shrink: 0;
  margin: 1%;
  width: 18%;
  height: 230px;
  border: 1px solid rgb(230, 230, 230);
  display: flex;
  flex-direction: column;
  .el-image {
    width: 100%;
    flex: 1;
  }
  .item-info {
    padding-left: 10px;
    height: 60px;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
  }
  .item-action {
    border-top: solid 1px rgb(230, 230, 230);
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: space-around;
    cursor: pointer;
    .anticon:hover {
      color: #0188fb;
    }
  }
}
</style>
